/**
 * Created by Administrator on 2017/8/28 0028.
 */

$(function () {

/*逻辑判断*/
            //首页搜索跳转  *******   获取浏览器里面的值跳转
    var searchUrl = window.location.href;
    var searchData = searchUrl.split("="); //截取 url中的“=”,获得“=”后面的参数
    var searchText = decodeURI(searchData[1]); //decodeURI解码

            //商品分类跳转 ******  获取浏览器里面的id
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }
    var gooID = GetQueryString("businessId") || 0;
/*逻辑结束*/
    if (gooID == 999999){
        $.ajax({
            url:BaseUrl+ "/api/good/list",
            type:"get",
            success:function(response) {
                var goodsHtml = template("goolist",response);
                $(".product-ul").html(goodsHtml);
            },
            error:function() {
                alert("系统繁忙，请稍后再试...");
            }
        });
    }else  if (gooID!=0){
        $.ajax({
            url:BaseUrl+ "/api/good/list?typeId="+gooID,
            type:"get",
            success:function(response) {
                var goodsHtml = template("goolist",response);
                $(".product-ul").html(goodsHtml);
            },
            error:function() {
                alert("系统繁忙，请稍后再试...");
            }
        });
    }else if(searchText){
        $.ajax({
            url:BaseUrl +'/api/good/list?name='+searchText+'&page=1&limit=50',
            type:"get",
            success:function(response) {
                var goodsHtml = template("goolist",response);
                $(".product-ul").html(goodsHtml);
            },
            error:function() {
                alert("系统繁忙，请稍后再试...");
            }
        });
    }else{
        $.ajax({
            url:BaseUrl+ "/api/good/list",
            type:"get",
            success:function(response) {
                var goodsHtml = template("goolist",response);
                $(".product-ul").html(goodsHtml);
            },
            error:function() {
                alert("系统繁忙，请稍后再试...");
            }
        });
    };
    // 一级分类文字替换
   $(document).ready( function(){
        $.ajax({
            url:BaseUrl +"/api/index/goodType/list",
            type:"get",
            success:function(allTypes) {
                console.log(allTypes);
                var sort_data = allTypes.data;
                for (var i = 0; i < sort_data.length; i++){
                    var sort_dataID = sort_data[i].id;
                    if (gooID == sort_dataID){
                        var goodsdata_name = sort_data[i].name;
                        $('#sort_goodsName').text(goodsdata_name);


                            //品牌图片**开始****
                        $.ajax({
                            url:BaseUrl+ '/api/good/brand?goodTypeId='+gooID+'page=1&limit=999',
                            type:"get",
                            success:function(response) {
                                console.log(response);
                                var goodsHtml = template("brand",response);
                                $("#av-collapse").html(goodsHtml);
                            },
                            error:function() {
                                alert("系统繁忙，请稍后再试...");
                            }
                        });
                            //品牌图片**结束*****



                    }else if(gooID == 999999){
                        $('#sort_goodsName').text('全部商品');
                    }
                }
            }
        });
//本页面点击商品搜索
       $("#seek_button").click(function () {
           var goods_value = $('#seek_shop').val();
           var newgoods_value = goods_value.split(" ").join("");
           if(newgoods_value.length == 0){
               $('#sort_goodsName').text('全部商品');
           }else {
               $('#sort_goodsName').text(goods_value);
           };

           $.ajax({
               url:BaseUrl +'/api/good/list?name='+goods_value+'&page=1&limit=50',
               type:"get",
               success:function(response) {
                   var goodsHtml = template("goolist",response);
                   $(".product-ul").html(goodsHtml);
               },
               error:function() {
                   alert("系统繁忙，请稍后再试...");
               }
           });
       });
    });

// 品牌点击更多效果实现html
    $("#avo-more").click(function() {
        var tex =  $("#avo-more em").text();
        console.log(tex);
        $(".navAtttrs .showLogo ul").toggleClass("overflow-y-scroll");
        $(".attrValues ul.av-collapse").toggleClass("overflow-hidden");
        if(tex =="收起"){
            tex="更多";
            $("#avo-more em").text(tex);
            $("#avo-more i").attr("class","sc sc-gengduo");
        }else if(tex =="更多"){
            tex="收起";
            $("#avo-more em").text(tex);
            $("#avo-more i").attr("class","sc sc-shouqi");
        }
    });
});
